<template>
	<view class="content">

		<view class="consels">
			<view class="logo-content">
				<image class="logo" src="/static/logonew.png" mode=""></image>
			</view>
			<view class="input-content">
				<view class="input-item ">
					<picker @change="bindPickerChange" :value="index" :range="array">
						<view class="pick-lab" style="width:140rpx">
							<text>{{ array[index] }}</text>
							<u-icon name="arrow-down"></u-icon>
						</view>
					</picker>
					<u-input type="text" style="width: 100%;" focus v-model="username" placeholder="请输入编号">
					</u-input>
				</view>
				<view class="input-item">
					<view style="width:140rpx">
						<image class="icon-input-code" src="../../static/code.png" mode=""></image>
					</view>
					<u-input type="password" style="width: 100%;" v-model="password" placeholder="请输入密码"></u-input>
				</view>
			</view>
			<view class="btn-row mt20">
				<u-button type="primary" class="primary mb20" @click="formSubmit">登录</u-button>
				<view class="flex mt20 crosswise">
					<view class="flex crosswise">
						<u-checkbox v-model="checked" shape="circle" active-color="#E9B461">
							<view class="agreement-text tcp-text u-flex u-col-center">
								<view class="tcp-text u-flex u-col-center">
									<view @tap.stop="$navTo('./userDeal')">《用户协议》</view>
									<!-- 与
									<view @tap.stop="$navTo('./privacyDeal')">《隐私协议》</view> -->
								</view>
							</view>
						</u-checkbox>
					</view>
					<view @click="fogetPsd" class="u-text-right">忘记密码 ？</view>
				</view>

			</view>

		</view>

		<view class="zhezhao">
			<image src="../../static/otherbg.png"></image>
		</view>
		<!-- <login :loadModal="loadModal" @hidelogin="hidelogin"></login> -->
	</view>
</template>
<script>
	export default {
		components: {

		},
		data() {
			return {
				username: '',
				password: '',
				// array: ['用户', '员工', '安全员', '协管员'],
				array: [],
				index: 0,
				identity: '', //登录者身份 yuangong/123456
				checked: false,
				loadModal: false,
				code:''
			};
		},
		onLaunch() {

		},
		onShow() {
			this.Login();
			// this.getCode()
		},
		methods: {
			getCode() {
				uni.login({
					success: res => {
						if (res.code) {
							this.code = res.code;
						} else {
							uni.showToast({
								title:'用户未授权code',
								icon:'none'
							})
						}
					},
					fail: res => { 
						uni.showToast({
							title:'用户授权失败',
							icon:'none'
						})
					}
				});
			},
			hidelogin(type) {
				console.log(type)
				this.loadModal = false;
			},
			checkboxChange(e) {
				console.log(e);
			},
			//登录
			Login() {
				let that = this

				that.$request.request({
					url: '/index/meke',
					method: 'GET',

				}).then(res => {
					// console.log(res.data.code)
					if (res.data.code == 0) {
						that.array = ['用户'];
					}
					if (res.data.code == 1) {
						that.array = ['用户', '员工', '安全员', '协管员'];
					}
				})
			},

			fogetPsd() {
				uni.navigateTo({
					url: 'fogetPsd'
				})
			},

			bindPickerChange(e) {
				this.index = e.target.value;
			},
			formSubmit() {
				console.log(this.index)
				if (this.index == 0) {
					this.identity = '/user/login';
				} else if (this.index == 1) {
					this.identity = '/user/staff_login';
				} else if (this.index == 2) {
					this.identity = '/user/safer_login';
				} else if (this.index == 3) {
					this.identity = '/user/assistant_login';
				}
				// this.identity = '/user/assistant_login';
				console.log(this.identity)
				// return
				if (this.username.length === 0) {
					uni.showToast({
						icon: 'none',
						title: '请填写帐号'
					});
					return;
				}
				if (this.password.length === 0) {
					uni.showToast({
						icon: 'none',
						title: '请填写密码'
					});
					return;
				}
				if (!this.checked) {
					return this.$showToast('请先阅读并同意协议')
				}
				uni.showLoading({
					mask: true,
					title: '正在登录...'
				});
				this.$u.post(this.identity, {
					account: this.username,
					password: this.password
				}).then(res => {
					console.log(res)
					if (res.code == 1) {
						uni.setStorage({
							key: 'token',
							data: res.data.userinfo.token
						})
						uni.setStorage({ //缓存配置信息
							key: 'userinfo',
							data: res.data.userinfo
						})
						uni.showToast({
							title: '登录成功',
							icon: true
						})

						if (this.index == 0) {
							uni.showLoading({
								mask: true,
								title: '正在登录...'
							});
							uni.setStorage({
								key: 'login',
								data: '用户'
							})
							setTimeout(() => {
								uni.hideLoading()
								uni.redirectTo({
									url: '/pages/index/index'
								});
							}, 1000)
						}
						if (this.index == 1) {
							uni.showLoading({
								mask: true,
								title: '正在登录...'
							});
							uni.setStorage({
								key: 'login',
								data: '员工'
							})
							setTimeout(() => {
								uni.hideLoading()
								uni.redirectTo({
									url: '/pagesB/index/index'
								})
							}, 1000)

						}
						if (this.index == 2) {
							uni.setStorage({
								key: 'login',
								data: '安全员'
							})
							uni.showLoading({
								mask: true,
								title: '正在登录...'
							});
							setTimeout(() => {
								uni.hideLoading()
								uni.redirectTo({
									// url: '/pagesC/index/volunteerindex',
									url: '/pagesA/index/volunteerindex'
								})
							})
						}
						if (this.index == 3) {
							uni.setStorage({
								key: 'login',
								data: '协管员'
							})
							uni.showLoading({
								mask: true,
								title: '正在登录...'
							});
							setTimeout(() => {
								uni.hideLoading()
								uni.redirectTo({
									url: '/pagesA/index/volunteerindex'
								})
							})
						}
					} else {
						uni.showLoading({
							mask: true,
							title: '正在登录...'
						});
						setTimeout(() => {
							uni.showToast({
								title: '登录失败',
								icon: 'none'
							})
							uni.hideLoading()
						}, 1000)
					}
				})

			},

		}
	};
</script>
<style scoped lang="less">
	.crosswise {
		align-items: center;
		justify-content: space-between;
	}

	.primary {
		height: 80rpx;
		width: 100%;
		color: #fff;
		background-color: #007aff;
	}

	.pl20 {
		padding-left: 20rpx;
	}

	.content {
		height: 100vh;
		background-size: 100% 100%;
		background-repeat: no-repeat;
		padding: 20rpx 40rpx;
	}

	.btn-row {

		view {
			color: #FFFFFF;
			text-align: right;
		}
	}

	.consels {
		position: relative;
		z-index: 2;
	}

	.zhezhao {
		position: absolute;
		top: 0;
		left: 0;
		height: 100vh;
		width: 100%;
		z-index: 0;

		image {
			height: 100%;
			width: 100%;
		}
	}

	.logo-content {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin: 0 auto;
		padding: 200rpx 0 100rpx 0;

		.logo {
			width: 200rpx;
			height: 200rpx;
			border-radius: 20rpx;
		}

		.name {
			font-size: 48rpx;
			font-weight: 700;
			color: #fff;
		}
	}

	.input-item {
		display: flex;
		align-items: center;
		box-sizing: border-box;
		width: 100%;
		padding: 18rpx 30rpx;
		margin: 0 auto 20rpx;
		border: 2rpx solid #0d6dc1;
		border-radius: 16rpx;
		background-color: #fff;
		// input .placeholder{
		// 	font-size: 32rpx;
		// 	color: #999;

		// }
		.pick-lab {
			color: #0d6dc1;
			padding: 0 20rpx 0 0;
			// border-right: 2rpx solid #e5e5e5;
			height: 48rpx;
			line-height: 48rpx;

			text {
				vertical-align: middle;
			}

			.m-icon {
				font-size: 30rpx;
			}
		}

		.icon-input {
			width: 50rpx;
			height: 50rpx;
		}

		.icon-input-code {
			width: 40rpx;
			height: 40rpx;
			margin: 0 6rpx;
		}
	}

	.action-row {
		display: flex;
		flex-direction: row;
		justify-content: center;
	}

	.action-row navigator {
		color: #0d6dc1;
		padding: 0 10px;
	}
</style>
